package main.java.com.yc.navy.snacknet.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yc.navy.snacknet.bean.CartInfo;
import com.yc.navy.snacknet.bean.CartInfoDTO;
import com.yc.navy.snacknet.bean.MemberInfo;
import com.yc.navy.snacknet.enums.ResultEnum;
import com.yc.navy.snacknet.service.ICartInfoService;
import com.yc.navy.snacknet.util.ConstantIfo;
import com.yc.navy.snacknet.vo.ResultVO;

@RestController
@RequestMapping("/cart")
public class CartInfoController {
	@Autowired
	private ICartInfoService cartService;
	
	/**
	 * 查询当前登录用户的购物车信息
	 * @param session
	 * @return
	 */
	@GetMapping("/list")
	public ResultVO list(HttpSession session) {
		Object obj = session.getAttribute(ConstantIfo.CURRENTLOGINMEMBER);
		if(obj == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		MemberInfo mf = (MemberInfo) obj;
		List<CartInfo> list = cartService.findByMno(mf.getMno());
		if(list != null && !list.isEmpty()) {
			return new ResultVO(ResultEnum.SUCCESS, list);
		}
		return new ResultVO(ResultEnum.DATA_NULL);	
	}
	
	@GetMapping("/finds")
	public ResultVO finds(HttpSession session) {
		Object obj = session.getAttribute(ConstantIfo.CURRENTLOGINMEMBER);
		if(obj == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		MemberInfo mf = (MemberInfo) obj;
		List<CartInfoDTO> list = cartService.finds(mf.getMno());
		if(list != null && !list.isEmpty()) {
			return new ResultVO(ResultEnum.SUCCESS, list);
		}
		return new ResultVO(ResultEnum.DATA_NULL);	
	}
	
	@PostMapping("/findByCnos")
	public ResultVO findByCnos(String cnos) {
		List<CartInfoDTO> list = cartService.findByCnos(cnos.split(","));
		if(list != null && !list.isEmpty()) {
			return new ResultVO(ResultEnum.SUCCESS, list);
		}
		return new ResultVO(ResultEnum.DATA_NULL);		
	}
	
	@PostMapping("/del")
	public ResultVO del(String cno) {
		int result = cartService.deleteByCno(cno);
		if(result > 0) {
			return new ResultVO(ResultEnum.SUCCESS);
		}
		return new ResultVO(ResultEnum.ERROP);		
	}
	
	@PostMapping("/add")
	public ResultVO add(CartInfo cf ,HttpSession session) {
		Object obj = session.getAttribute(ConstantIfo.CURRENTLOGINMEMBER);
		if(obj == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		MemberInfo mf = (MemberInfo) obj;
		String cno = UUID.randomUUID().toString();
		cf.setCno(cno);
		cf.setMno(mf.getMno());
		int result = cartService.add(cf);
		if(result == -100) {
			return new ResultVO(ResultEnum.CHECK_ERROR);
		}
		if(result > 0) {
			return new ResultVO(ResultEnum.SUCCESS);
		}
		return new ResultVO(ResultEnum.ERROP);		
	}
	
	@PostMapping("/update")
	public ResultVO update(CartInfo cf) {
		int result = cartService.update(cf);
		if(result == -100) {
			return new ResultVO(ResultEnum.CHECK_ERROR);
		}
		if(result > 0) {
			return new ResultVO(ResultEnum.SUCCESS);
		}
		return new ResultVO(ResultEnum.ERROP);		
	}
}
